using System;
using System.Collections.Generic;
using System.Text;

namespace LibGreen.Jabber.ProtocolElements.IqElements
{
    /// <summary>
    /// Defines an &lt;iq&gt; element with the &lt;query xmlns="jabber:iq:roster"&gt; subtype
    /// </summary>
    public class IqRosterElement
    {
        /// <summary>
        /// The xmlns of the IQ query element for roster operations
        /// </summary>
        private const string XMLNS = "jabber:iq:roster";

        /// <summary>
        /// Creates a new &lt;iq&gt;&lt;query /&gt;&lt;/iq&gt; stanza to request the client's roster at startup
        /// </summary>
        /// <param name="from">The <see cref="Jid"/> of the client</param>
        /// <returns>A new &lt;iq&gt; element</returns>
        public static Element Create(Jid from)
        {
            Element parent = IqElement.Create(IqType.Get, from.ToString());
            parent.Children.Add(IqQueryElement.Create(XMLNS));
            return parent;
        }

        /// <summary>
        /// Creates a new &lt;iq&gt;&lt;query /&gt;&lt;/iq&gt; stanza to add or update an item on the client's roster
        /// </summary>
        /// <param name="from">The <see cref="Jid"/> of the client</param>
        /// <param name="resource">The <see cref="Jid"/> of the resource to add or update</param>
        /// <param name="name">A name with which to label the resource</param>
        /// <param name="groups">A list of zero or more roster groups in which to place the resource</param>
        /// <returns>A new &lt;iq&gt; element</returns>
        public static Element Create(Jid from, Jid resource, string name, List<string> groups)
        {
            // Create the relevent elements...
            Element parent = IqElement.Create(IqType.Set, from.ToString(false));
            Element query = IqQueryElement.Create(XMLNS);
            Dictionary<string, string> itemAttributes = new Dictionary<string, string>(3);
            itemAttributes["jid"] = resource.ToString(false);
            if (!String.IsNullOrEmpty(name))
            {
                itemAttributes["name"] = name;
            }
            Element item = Element.CreateElement("item", itemAttributes);
            foreach (string group in groups)
            {
                item.Children.Add(Element.CreateElement("group", new Dictionary<string, string>(), group));
            }
            // Chain them together...
            query.Children.Add(item);
            parent.Children.Add(query);
            // And awaaaaay we go
            return parent;
        }

        /// <summary>
        /// Creates a new &lt;iq&gt;&lt;query /&gt;&lt;/iq&gt; stanza to remove an item from the client's roster
        /// </summary>
        /// <param name="from">The <see cref="Jid"/> of the client</param>
        /// <param name="resource">The <see cref="Jid"/> of the resource to remove</param>
        /// <returns>A new &lt;iq&gt; element</returns>
        public static Element Create(Jid from, Jid resource)
        {
            // Create the relevent elements...
            Element parent = IqElement.Create(IqType.Set, from.ToString(false));
            Element query = IqQueryElement.Create(XMLNS);
            Dictionary<string, string> itemAttributes = new Dictionary<string, string>(3);
            itemAttributes["jid"] = resource.ToString(false);
            itemAttributes["subscription"] = "remove";
            Element item = Element.CreateElement("item", itemAttributes);
            // Chain them together...
            query.Children.Add(item);
            parent.Children.Add(query);
            // And awaaaaay we go
            return parent;
        }
    }
}
